// 导入 http 模块
import http from 'wechat-http'
/**
 * 配置接口基础路径
 */
http.baseURL = 'https://live-api.itheima.net'
/**
 * 挂载方法到全局
 */
http.intercept.request = (params) => {
  // 过滤接口返回的数据
  const app = getApp()
  if (app.token) {
    params.header = {
      Authorization: `Bearer ${app.token}`,
      ...params.header,

    }
  }
  return params
}
http.intercept.response = async (res) => {
  // 过滤接口返回的数据
  // console.log(res);
  if (res.statusCode === 401) {
    // 调用app实列
    const app = getApp()
    // 发送亲求
    const res = await wx.http({
      url: '/refreshToken',
      method: 'post',
      headers: {
        Authorization: 'Bearer' + app.refreshToken
      }
    })
    console.log(res);
    const { token, refreshToken } = res.data
    app.setToken(token, refreshToken)

  }
  return res.data
}
wx.http = http
/**
 * 模块导出
 */
export default http
